// print('start dev Pre js...\n');

// print('pre vir dev js...\n');

var deta = function (devSn, collName, maxValue) {
    var result = 0;

    var devIdObj = db.queryOne("select id,sn,name from iot_dev where sn='"+devSn+"'");

    var devId = devIdObj?devIdObj.id:null;
    // print('devId=' + devId);
    if(!devId){
        return result;
    }

    var lastData = 0;
    if(theTime){
        lastData = db.queryOne("select dvalue,DATE_FORMAT(the_time,'%Y-%m-%d %H') theTime from iot_dev_data_win win " +
            " where 1=1" +
            "   and dev_id=" + devId +
            "   and data_type='" + collName + "'" +
            "   and date_add(STR_TO_DATE('"+theTime+"','%Y-%m-%d %H'), interval 0  hour)=DATE_FORMAT(win.the_time,'%Y-%m-%d %H') " +
            // "   and STR_TO_DATE('"+theTime+"','%Y-%m-%d %H')=DATE_FORMAT(win.the_time,'%Y-%m-%d %H') " +
            " order by the_time desc " +
            " limit 1 " );
    }else{
        lastData = db.queryOne("select dvalue,DATE_FORMAT(the_time,'%Y-%m-%d %H') theTime from iot_dev_data_win win " +
            " where 1=1" +
            "   and dev_id=" + devId +
            "   and data_type='" + collName + "'" +
            "   and date_add(STR_TO_DATE(DATE_FORMAT(now(),'%Y-%m-%d %H'),'%Y-%m-%d %H'), interval 0 hour)=DATE_FORMAT(win.the_time,'%Y-%m-%d %H') " +
            " order by the_time desc " +
            " limit 1 " );

    }


    var lastDate = lastData.theTime;
    // print('lastDate=' + lastDate);

    var lastDataPre = db.queryOne("select dvalue,DATE_FORMAT(the_time,'%Y-%m-%d %H') theTime from iot_dev_data_win win " +
        " where 1=1" +
        "   and dev_id=" + devId +
        "   and data_type='" + collName + "'" +
        "   and date_add(STR_TO_DATE('"+lastDate+"','%Y-%m-%d %H'),interval -1 hour)>=DATE_FORMAT(win.the_time,'%Y-%m-%d %H') " +
        " order by the_time desc " +
        " limit 1 " );

    print('resultCal Obj ... lastData=' + lastData );
    print('resultCal Obj ... lastDataPre=' + lastDataPre);
    if(!lastDataPre){
        lastDataPre = 0;
    }
    if(lastData && lastDataPre && lastData.dvalue && lastDataPre.dvalue){
        result = lastData.dvalue - lastDataPre.dvalue;

        // 郑沐兴的意思：如果result为负数，说明lastDataPre.dvalue已经达到100万，result直接取lastData.dvalue
        if (result < 0) {
            result = lastData.dvalue;
        }
    }

    // print('result=' + result);
    return result;
};

/**
 * 综合判断设备[运行状态]属性: eg: runstate('A001.运行状态',['A001.低压故障','A001.运行故障'])
 * note. 所有数据读取时间范围：2min之内
 *
 * @param commonStateProp 正常状态 属性，一旦[故障们属性] 合法，该值弃用 ，eg： 'A001.运行状态',
 * @param arrErrorStateProp 故障们 属性【数组】，判断优先级随传参的顺序，一旦合法，余下故障属性弃用不判断，eg：['A001.低压故障','A001.运行故障']
 */
var runstate = function (commonStateProp, arrErrorStateProp) {

    if (!Array.isArray(arrErrorStateProp)) {
        print('第二个参数错误，需要传参为数组，如为空，写中括号[],' + commonStateProp);
        return
    }

    for (var i = 0; i < arrErrorStateProp.length ; i++) {
        var curErrorText = arrErrorStateProp[i]
        var curErrorTextList = $.doSplit(curErrorText,'.')
        var curErrorSql = "select dvalue " +
            "from iot_dev dev " +
            "left join iot_dev_cur_data_win win on win.dev_id = dev.id " +
            "where win.uptime > SUBDATE(now(),interval 120 second) and dev.sn = '" + curErrorTextList[0] +"' and win.data_type = '" + curErrorTextList[1] + "'"
        var curErrorResult = db.queryOne(curErrorSql);
        // 只处理查到数据的情况，不处理没查到数据的情况
        if ($.ifEmpty(JSON.stringify(curErrorResult))) {
            if (parseInt(curErrorResult.dvalue) == 1) {
                return 2;
            }
        }
    }


    var commonSplitList = $.doSplit(commonStateProp,'.')

    var commonSql = "select dvalue " +
        "from iot_dev dev " +
        "left join iot_dev_cur_data_win win on win.dev_id = dev.id " +
        "where win.uptime > SUBDATE(now(),interval 120 second) and dev.sn = '" +commonSplitList[0] +"' and win.data_type = '" + commonSplitList[1] + "'"

    var commonResult = db.queryOne(commonSql);

    if (!$.ifEmpty(JSON.stringify(commonResult))) {
        //数据库查不到数据
        return 2;
    }

    return parseInt(commonResult.davlue);

};


/**
 * 综合判断设备[运行状态]属性: eg: state(A001.运行状态,[A001.低压故障,A001.运行故障])
 *
 *
 * @param commonStateProp
 * @param arrErrorStateProp
 */
var state = function (commonStateProp, arrErrorStateProp) {


    if (!Array.isArray(arrErrorStateProp)) {
        print('第二个参数错误，需要传参为数组，如为空，写中括号[],' + commonStateProp);
        return
    }


    for (var i = 0; i < arrErrorStateProp.length; i++) {

        var value = arrErrorStateProp[i]

        if (value == undefined) {
            continue
        }
        if (parseInt(value) == 1) {
            return 2;
        }
    }

    var commonValue = commonStateProp;
    if (commonValue == undefined) {
        // 模糊，如果找不到这个数据，原因是什么？可能误以为是真的故障问题，可能是找不到数据的问题.....
        print("找不到数据undefined......返回故障状态2");
        return 2;
    }
    return parseInt(commonValue);
};
